#! /usr/bin/env python
#
# A script that creates an HDF5 file of a particular user's
# experiments. It can be limited to a single experiment using
# the -e command-line option.
#
# Usage:
#   create_user_hdf5.py [-e <experiment name>] user_name output_filename
#
# Mike Dorey  2011-03-28


import os
import sys
import optparse
import database_access
import dust_events_hdf5


# defaults 
FILE_VERSION = 'A1'
DB_SERVER = 'localhost'
DB_DATABASE = 'ccldas'
DB_DEFAULTS_FILENAME = os.path.join(os.environ['HOME'], '.my.cnf')


def main(cursor, user_name, experiment_names, output_filename, file_version):
    '''Creates an HDF5 file of a user's experiments.

    Arguments
        cursor - a database cursor
        user_name - name of a user
        experiment_names - list of experiment names
        output_filename - name of the HDF5 file to create
        file_version - version of the file
    '''
    experiments = []
    for experiment_name in experiment_names:
        experiment = database_access.fetch_dust_experiment(cursor, user_name, 
                     experiment_name)
        experiments.append(experiment)
    hdf5_file = dust_events_hdf5.open_hdf5_file(output_filename, file_version)
    try:
        user_group = dust_events_hdf5.create_user_group(hdf5_file, user_name)
        dust_events_hdf5.add_experiments_to_user_group(user_group, experiments)
    finally:
        hdf5_file.close()
            

def get_options_args():
    '''Returns a tuple of (options, user name, output filename), which are the options and
    arguments specified on the command line.
    '''
    usage = 'Usage: %prog [options] <user name> <output file name>'
    option_parser = optparse.OptionParser(usage=usage)
    option_parser.add_option("-e", "--experiment", dest="experiment",
                             help="name of the experiment")
    option_parser.add_option("-s", "--server", dest="db_server", default=DB_SERVER,
                             help="name of the database server")
    option_parser.add_option("-d", "--database", dest="database", default=DB_DATABASE,
                             help="name of the database")
    option_parser.add_option("-v", "--version", dest="version", default=FILE_VERSION,
                             help="version of the output file")
    option_parser.add_option("-f", "--db_defaults_filename", dest="db_defaults_filename",
                             default=DB_DEFAULTS_FILENAME,
                             help="name of the database defaults file")
    (options, args) = option_parser.parse_args()
    assert len(args) == 2, 'Improper usage. Did not specify both the user and the output filename.'
    return (options, args[0], args[1])


if __name__ == "__main__":
    (options, user_name, output_filename) = get_options_args()
    (connection, cursor) = database_access.get_connection_and_cursor(options.db_server, 
                       options.database, options.db_defaults_filename)
    try:
        if options.experiment is not None:
            main(cursor, user_name, [options.experiment], output_filename, options.version)
        else:
            experiment_names = database_access.fetch_user_experiment_names(cursor, user_name)
            main(cursor, user_name, experiment_names, output_filename, options.version)
    finally:
        connection.close()
